package com.douing.ad.dsp.bidding.tj.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSON;

public class CommonMethod {
    /**
     * 获取任意方法的参数值，并放到MyField里面
     * @param element
     * @return
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws InstantiationException
     */
    public static List<MyField> getElementArrayList (Object element) throws SecurityException,  
    NoSuchMethodException, IllegalArgumentException,  
    IllegalAccessException, InvocationTargetException, InstantiationException {
        // 获取实体类的所有属性，返回Field数组 
        Field[] field = element.getClass().getDeclaredFields();
        // 获取属性的名字  
        String[] modelName = new String[field.length];  
        String[] modelType = new String[field.length]; 
        List<MyField> elementArray = new ArrayList<MyField>();
        for (int i = 0; i < field.length; i++)  
        {  
            // 获取属性的名字  
            String name = field[i].getName();  
            modelName[i] = name;  
            // 获取属性类型  
            String type = field[i].getGenericType().toString();  
            modelType[i] = type;  
              
            //关键。。。可访问私有变量  
            field[i].setAccessible(true);  
  
            // 将属性的首字母大写  
            String name2 = name.replaceFirst(name.substring(0, 1), name.substring(0, 1)  
                    .toUpperCase());  
            
            if (type.equals("int")){  
                Method m = element.getClass().getMethod("get" + name2);  
                Integer value = (Integer) m.invoke(element);  
                if (value != null)  
                {  
                    elementArray.add(new MyField(name, value));
                }  
            }
            
        }
        return elementArray;
    }
    /**
     * 把MyField里面的数值传给任意DataElement
     * @param res
     * @return
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws InstantiationException
     * @throws NoSuchFieldException
     */
    
    public static DataElement setDataElementValue(List<MyField> res) throws SecurityException,  
    NoSuchMethodException, IllegalArgumentException,  
    IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        DataElement element = new DataElement();
        // 获取实体类的所有属性，返回Field数组 
        Field[] field = element.getClass().getDeclaredFields();
        // 获取属性的名字  
        String[] modelName = new String[field.length];  
        String[] modelType = new String[field.length]; 
        for (int i = 0; i < field.length; i++)  
        {  
            // 获取属性的名字  
            String name = field[i].getName();  
            modelName[i] = name;  
            // 获取属性类型  
            String type = field[i].getGenericType().toString();  
            modelType[i] = type;  
              
            //关键。。。可访问私有变量  
            field[i].setAccessible(true);  
            if (type.equals("int")){  
                Field f= element.getClass().getDeclaredField(name); 
                f.setAccessible(true);
                 
                for (MyField myField : res) {
                  if(myField.getName().equals(name)){
                      f.set(element, myField.getValue());
                  }  
                }    
            } 
        
        }
        return element;
    }
    
    /**
     * 两个实体类对应的step相加
     * @param dataElement
     * @param nodeData
     * @return
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws InstantiationException
     */
    public static List<MyField> addStepToCount(DataElement dataElement,NodeData nodeData) throws SecurityException,  
    NoSuchMethodException, IllegalArgumentException,  
    IllegalAccessException, InvocationTargetException, InstantiationException {
        List<MyField> dataelementArray = getElementArrayList(dataElement);
        List<MyField> nodeDataArray = getElementArrayList(nodeData);
        List<MyField> res = Join(dataelementArray, nodeDataArray);
        return res; 
    }
    
    /**
     * 判断是否包含数字
     * @param content
     * @return
     */
    public static boolean HasDigit(String content) {  
        boolean flag = false;  
        Pattern p = Pattern.compile(".*\\d+.*");  
        Matcher m = p.matcher(content);  
        if (m.matches()) {  
            flag = true;  
        }  
        return flag;  
    }  
    /**
     * 把list2加到对应的list1
     * @param list1
     * @param list2
     * @return
     */
    public static List<MyField> Join(List<MyField> list1, List<MyField> list2) {
        List<MyField> res = new ArrayList<MyField>();
        List<MyField> list2_copy = new ArrayList<MyField>(list2);
        for (MyField field1 : list1) {
            boolean hasSameOne = false;
            for (MyField field2 : list2) {
                if (field1.getName().contains(field2.getName())) {
                    res.add(field1.add(field2));
                    list2_copy.remove(field2);
                    hasSameOne = true;
                }
            }
            if (!hasSameOne)
                res.add(field1);
        }
        return res;
    }
       
    /**
     * 测试方法
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        NodeData nodeData = new NodeData();
        nodeData.setStep1(1);
        nodeData.setStep2(1);
        nodeData.setStep3(1);
        nodeData.setStep4(1);
        DataElement dataElement = new DataElement();
        dataElement.setStep4Count(1);
        dataElement.setStep5Count(1);
        dataElement.setStep6Count(1);
        dataElement.setStep7Count(1);
        dataElement.setRequestCount(55);
        dataElement.setTimeOutCount(66);
        System.out.print(JSON.toJSONString(nodeData));
        System.out.println("\n");
        System.out.print(JSON.toJSONString(dataElement));
        System.out.println("\n");
        List<MyField> dataelementArray = addStepToCount(dataElement,nodeData);
        DataElement dataElement2 = setDataElementValue(dataelementArray);
        System.out.print(JSON.toJSONString(dataElement2));
    }
    
}

